layui.use('table', function () {


    var table = layui.table;
    //方法渲染
    table.render({

        elem: '#shopOrders',  //绑定table表格
        height: 500,
        url: '/shopOrdersCount' //数据接口
        , page: true //开启分页
        , cols: [
            [ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: '订单ID', width: 100, sort: true, fixed: 'left'}
                , {field: 'userId', title: '用户I', width: 100}
                , {field: 'receiverName', title: '收货人', width: 100}
                , {field: 'receiverMobile', title: '收货人手机号', width: 130}
                , {field: 'receiverAddress', title: '收货地址', width: 130}
                , {field: 'totalAmount', title: '订单总价格', width: 100}
                , {field: 'postAmount', title: '配送费', width: 100}
                , {field: 'realPayAmount', title: '实际支付总价格', width: 130}
                , {field: 'leftMsg', title: '买家留言', width: 130}
                , {
                field: 'payMethod',
                title: '支付方式',

                width: 130,templet: function (res) {//用户角色。0：正常；1：禁用
                    if (res.payMethod == '1') {
                        return '<span >微信支付</span>'
                    } else if (res.payMethod == '2') {
                        return '<span >支付宝支付</span>'
                    }
                }}

                , {field: 'leftMsg', title: '买家留言', width: 130,templet: function (res) {//用户角色。0：正常；1：禁用
                    if (res.isComment == '1') {
                        return '<span >已评价</span>'
                    } else if (res.isComment == '0') {
                        return '<span >未评价</span>'
                    }}}
                , {
                field: 'createdTime',
                title: '创建时间',
                width: 150,
                templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"
            }
                , {
                field: 'orderStatus',
                title: '订单状态',
                width: 110,
                templet: function (res) {//用户角色。0：正常；1：禁用
                    if (res.orderStatus == '10') {
                        return '<span >待付款</span>'
                    } else if (res.orderStatus == '20') {
                        return '<span >已付款</span>'
                    }else if (res.orderStatus == '30') {
                        return '<span >已发货</span>'
                    }else if (res.orderStatus == '40') {
                        return '<span >交易成功</span>'
                    }else if (res.orderStatus == '50') {
                        return '<span >交易关闭</span>'
                    }
                }}

                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 265}
            ]
        ]
    });

    table.on('tool(AOrders)', function (obj) {  //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data;   //获得当前行数据
        // console.log("del:::" + obj)
        if (obj.event === 'del') {

        } else if (obj.event === 'modify') {   //修改数据
            layer.open({
                type: 2,
                title: '查看订单信息'   //标题
                , area: ['500px', '300px']    //宽高
                , content: ['/lookOrders?id=' + data.id] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
            });
        } else if (obj.event === 'lookUser') {   //修改数据
            layer.open({
                type: 2,
                title: '查看评价信息'   //标题
                , area: ['500px', '300px']    //宽高
                , content: ['orderEvaluate?id=' + data.id] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
            });
        }else if(obj.event === 'tableData'){
            //免加载页面弹窗
            layer.open({
                type: 2,
                title: '订单处理'   //标题
                , area: ['300px', '200px']    //宽高
                , content: ['orderRefund?id=' + data.id] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
            });
        }


    });

    $('#orderBtnSearch').click(function () {
        var key = $('#orderSltKey').val();
        var value = $('#orderEdtSearch').val();
        table.reload('shoporders', {where: {type: key, keywords: value}});
    });
});


layui.use(['laydate'], function () {
    var $ = layui.$;
    var laydate = layui.laydate;
    var nowTime = new Date().valueOf();
    var max = null;

    var start = laydate.render({
        elem: '#start_time',
        type: 'datetime',
        format: 'yyyy-MM-dd HH:mm:ss',
        max: nowTime,
        done: function (value, date, endDate) {
            endMax = end.config.max;
            end.config.min = date; //最大时间为结束时间的开始值  
            end.config.min.month = date.month - 1;
            $("#start_time").val(value);
            searchData();
        }
    });

    var end = laydate.render({
        elem: '#end_time',
        type: 'datetime',
        max: 4073558400000,
        format: 'yyyy-MM-dd HH:mm:ss',
        min: nowTime,
        done: function (value, date) {
            if ($.trim(value) == '') {
                var curDate = new Date();
                date = {'date': curDate.getDate(), 'month': curDate.getMonth() + 1, 'year': curDate.getFullYear()};
            }
            start.config.max = date;//最小时间为开始时间的最大值  
            start.config.max.month = date.month - 1;
            $("#end_time").val(value);
            searchData();
        }
    })

});
// 退款事件
$("#Refund").on('click',function(){
    layer.closeAll();
});